package com.gexin.service.memberAcitivty;

import com.gexin.common.model.ApiResponse;
import com.gexin.common.utils.BigDecimalUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.activity.MemberBillShopLog;
import com.gexin.model.activity.MemberBillSpeedIntegral;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Random;

public class BillOfShopService {

    private MemberInfoService memberInfoService = new MemberInfoService();


    /**
     * 提货券转账
     *
     * @param authUserId
     * @param presentId
     * @param billOfPre
     * @return
     */
    public ApiResponse presentBillOfPre(String authUserId, String presentId, BigDecimal billOfPre) {
        ApiResponse apiResponse = memberInfoService.checkAuthtication(authUserId, presentId, BigDecimal.ZERO, billOfPre, 0);
        if (apiResponse.isState() == false) {
            return apiResponse;
        }


        MemberBillSpeedIntegral memberBillSpeedIntegral = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId(authUserId);
        MemberBillSpeedIntegral presendMemberBillSpeedIntegral = MemberBillSpeedIntegral.dao.getMemberBillSpeedIntegralByUserId(presentId);
        if (memberBillSpeedIntegral.getBillCoupon() == null) {
            return new ApiResponse(false, "可以转账为0");
        }
        BigDecimal billOfLadingIntMine = memberBillSpeedIntegral.getBillCoupon();
        if (BigDecimalUtils.lessThan(billOfLadingIntMine, billOfPre)) {
            return new ApiResponse(false, "转账金额超过本人提货券");
        }

//      赠送人的积分表以及日志
        billOfLadingIntMine = billOfLadingIntMine.subtract(billOfPre);
        memberBillSpeedIntegral.setBillCoupon(billOfLadingIntMine);
        memberBillSpeedIntegral.setUpdateTime(new Date().getTime());
        memberBillSpeedIntegral.update();
        MemberBillShopLog memberBillShopLog = new MemberBillShopLog();
        memberBillShopLog.setCommonUserIdAndPresentIdAndbillAndIncomeOrAndIncrementAndDesc(
                authUserId, presentId,null, 1,billOfLadingIntMine,billOfPre
                ,0, "提货券赠送");
        memberBillShopLog.save();

         if (presendMemberBillSpeedIntegral == null) {
            presendMemberBillSpeedIntegral = new MemberBillSpeedIntegral();
            presendMemberBillSpeedIntegral.setUserIdAndSpeedIntegralAndCoinAndBillCouponAndForecastAndRemainAndReverseAndDailyIncrement
            (presentId, BigDecimal.ZERO,BigDecimal.ZERO,billOfPre,BigDecimal.ZERO, BigDecimal.ZERO,
                    BigDecimal.ZERO, BigDecimal.ZERO);
            presendMemberBillSpeedIntegral.save();
        } else {
            java.math.BigDecimal presendBillCoupon = presendMemberBillSpeedIntegral.getBillCoupon();
            java.math.BigDecimal presendBillCouponSum = presendBillCoupon.add(billOfPre);
            presendMemberBillSpeedIntegral.setBillCoupon(presendBillCouponSum);
            presendMemberBillSpeedIntegral.setUpdateTime(new Date().getTime());
            presendMemberBillSpeedIntegral.update();
        }
        MemberBillShopLog presentMemberBillShopLog = new MemberBillShopLog();
        presentMemberBillShopLog.setCommonUserIdAndPresentIdAndbillAndIncomeOrAndIncrementAndDesc(
                presentId,null,authUserId,1,presendMemberBillSpeedIntegral.getBillCoupon(),
                billOfPre, 1, "提货券赠送");
        presentMemberBillShopLog.save();

        return new ApiResponse(true, "转账成功", 200);
    }

    /**
     * 转账验证码
     */
    public void veritfy() {

    }

    //发送验证码
    public ApiResponse sendVerificationCode(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return new ApiResponse("系统未检查到用户的手机号,请填写后重试！");
        }
        Random rand = new Random();
        String code = StringUtil.fillString(rand.nextInt(10000) + "", "0", 4, StringUtil.FILL_TYPE_LEFT);
//        MessageUtils.SendCode_New(phone, code, MessageUtils.loginId,getRequest());
        return new ApiResponse("");
    }

}
